
/*
SoftwareSerial.h (formerly NewSoftSerial.h) -
Multi-instance software serial library for Arduino/Wiring
-- Interrupt-driven receive and other improvements by ladyada
(http://ladyada.net)
-- Tuning, circular buffer, derivation from class Print/Stream,
multi-instance support, porting to 8MHz processors,
various optimizations, PROGMEM delay tables, inverse logic and
direct port writing by Mikal Hart (http://www.arduiniana.org)
-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
-- 20MHz processor support by Garrett Mace (http://www.macetech.com)
-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
The latest version of this library can always be found at
http://arduiniana.org.
*/

#ifndef _Serial_included_
#define _Serial_included_
#include <inttypes.h>
//#include <Stream.h>

#ifndef F_CPU
//
// Says to the compiler which is our clock frequency, permits the delay 
// functions to be very accurate
//
#define F_CPU 16000000UL
#endif

//
// The baud rate from boards.txt.
// The baudrate that we want to use
//
#define SERIAL_BAUD 9600   //115200 
//
// he formula that does all the required maths
//
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

#define SERIAL_RX  10
#define SERIAL_TX  11

#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2

#define _SS_MAX_RX_BUFF 64 // RX buffer size

#ifndef GCC_VERSION
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif

typedef struct {
  uint8_t _receivePin;
  uint8_t _receiveBitMask;
  volatile uint8_t *_receivePortRegister;
  uint8_t _transmitBitMask;
  volatile uint8_t *_transmitPortRegister;
  uint16_t _rx_delay_centering;
  uint16_t _rx_delay_intrabit;
  uint16_t _rx_delay_stopbit;
  uint16_t _tx_delay;
  uint32_t _write_error;
  uint16_t _buffer_overflow:1;
  uint16_t _inverse_logic:1;
} Serial_t;

extern void Serial_begin (Serial_t * this, uint8_t receivePin, uint8_t transmitPin, int inverse_logic, long speed);
extern int Serial_listen (Serial_t * this);
extern void Serial_end (Serial_t * this);
extern int Serial_isListening (Serial_t * this);
extern int Serial_overflow (Serial_t * this);
extern int Serial_peek (Serial_t * this);
extern size_t Serial_write (Serial_t * this, uint8_t byte);
extern int Serial_read (Serial_t * this);
extern int Serial_available (Serial_t * this);
extern void Serial_flush (Serial_t * this);
extern void Serial_setWriteError(Serial_t * this, int err);
extern int Serial_getWriteError (Serial_t * this);

extern size_t Serial_print_char (Serial_t * this, char c);
extern size_t Serial_println (Serial_t * this);
extern size_t Serial_println_char (Serial_t * this, char c);
extern size_t Serial_print_string (Serial_t * this, const char * str);
extern size_t Serial_println_string (Serial_t * this, const char * str);
extern size_t Serial_print_signed (Serial_t * this, long n, int base);
extern size_t Serial_println_signed (Serial_t * this, long n, int base);
extern size_t Serial_print_unsigned (Serial_t * this, unsigned long n, int base);
extern size_t Serial_println_unsigned (Serial_t * this, unsigned long n, int base);
extern size_t Serial_print_float (Serial_t * this, double number, uint8_t digits);
extern size_t Serial_println_float (Serial_t * this, double number, uint8_t digits);

#if 0
class SoftwareSerial : public Stream
{
private:
// per object data
uint8_t _receivePin;
uint8_t _receiveBitMask;
volatile uint8_t *_receivePortRegister;
uint8_t _transmitBitMask;
volatile uint8_t *_transmitPortRegister;
uint16_t _rx_delay_centering;
uint16_t _rx_delay_intrabit;
uint16_t _rx_delay_stopbit;
uint16_t _tx_delay;
uint16_t _buffer_overflow:1;
uint16_t _inverse_logic:1;
// static data
static char _receive_buffer[_SS_MAX_RX_BUFF];
static volatile uint8_t _receive_buffer_tail;
static volatile uint8_t _receive_buffer_head;
static SoftwareSerial *active_object;
// private methods
void recv();
uint8_t rx_pin_read();
void tx_pin_write(uint8_t pin_state);
void setTX(uint8_t transmitPin);
void setRX(uint8_t receivePin);
// private static method for timing
static inline void tunedDelay(uint16_t delay);
public:
// public methods
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
~SoftwareSerial();
void begin(long speed);
bool listen();
void end();
bool isListening() { return this == active_object; }
bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
int peek();
virtual size_t write(uint8_t byte);
virtual int read();
virtual int available();
virtual void flush();
using Print::write;
// public only for easy access by interrupt handlers
static inline void handle_interrupt();
};
// Arduino 0012 workaround
#undef int
#undef char
#undef long
#undef byte
#undef float
#undef abs
#undef round
#endif

#endif // 0
